package com.csgc.camera.biz.other.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View

import com.csgc.camera.base.BaseBindingActivity
import com.csgc.camera.constants.MaskConfig

import com.csgc.camera.events.MaskLocationEvent
import com.csgc.camera.storage.MaskPreference
import com.csgc.camera.util.clickWithTrigger
import com.cysd.spsyxj.R
import com.cysd.spsyxj.databinding.ActivityLocationStyleBinding

import com.drake.channel.sendEvent
import com.gyf.immersionbar.ktx.immersionBar
import com.hlyj.http.ad.NewGroMoreFeedAdManager
import com.hnyyac.ad.mds.IAdView
import com.hnyyac.ad.mds.IAdViewListener
import com.hnyyac.ad.mds.manager.AdManager
import com.hnyyac.ad.mds.manager.AdManager.AdLoadListener
import com.hnyyac.ad.mds.utils.BaseAdConstants

class LocationStyleActivity:BaseBindingActivity<ActivityLocationStyleBinding>(ActivityLocationStyleBinding::inflate),IAdViewListener {

    companion object{
        private const val key_province = "province"
        private const val key_city = "city"
        private const val key_district = "district"
        private const val key_street = "street"
        private const val key_snippet = "snippet"
        fun launch(context: Context,province:String,city:String,district:String,street:String,snippet:String){
                val intent = Intent(context,LocationStyleActivity::class.java)
                intent.putExtra(key_province,province)
                intent.putExtra(key_city,city)
                intent.putExtra(key_district,district)
                intent.putExtra(key_street,street)
                intent.putExtra(key_snippet,snippet)
                context.startActivity(intent)
        }
    }
    private val province:String by lazy {
        intent.getStringExtra(key_province)?:""
    }
    private val city:String by lazy {
        intent.getStringExtra(key_city)?:""
    }
    private val district:String by lazy {
        intent.getStringExtra(key_district)?:""
    }
    private val street:String by lazy {
        intent.getStringExtra(key_street)?:""
    }
    private val snippet:String by lazy {
        intent.getStringExtra(key_snippet)?:""
    }
    private var checkedStyle = 1
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        immersionBar {
            fitsSystemWindows(true)
            statusBarColor(R.color.white)
            statusBarDarkFont(true)
        }
        initView()
        initData()
        checkedStyle = MaskPreference.instance.getLocationStyle()
        updateCheckedStyle()
        initEvents()


    }


    private fun initView(){
        binding.tvLocationInfo2.text = "$city$snippet"
        binding.tvLocationInfo3.text = "$district$snippet"
        binding.tvLocationInfo4.text = "$province$city$snippet"
        binding.tvLocationInfo5.text = "$city$district$snippet"
        binding.tvLocationInfo6.text = "$district$street$snippet"
        binding.tvLocationInfo7.text = "$city$district$street$snippet"
        binding.tvLocationInfo8.text = "$province$city$district$street$snippet"
    }

    private fun initData() {
        //广告插入
        val newGromoreFeedAd: NewGroMoreFeedAdManager = NewGroMoreFeedAdManager(this, BaseAdConstants.FEEDAD_Id, binding?.frameContent, 66)
        newGromoreFeedAd.loadAd()
        showDoubleChaPin()
    }

    private fun initEvents(){
        binding.ivBack.clickWithTrigger { finish() }
        binding.stvLocationStyleSure.clickWithTrigger {
            MaskPreference.instance.setLocationStyle(checkedStyle)
            MaskConfig.locationStyle = checkedStyle
            MaskConfig.setLocationStyle(province,city,district, street, snippet)
            sendEvent(MaskLocationEvent(MaskConfig.maskLocation))
            finish()
        }

        binding.layoutLocation1.clickWithTrigger {
            if(checkedStyle != 1){
                checkedStyle = 1
                updateCheckedStyle()
            }

        }
        binding.layoutLocation2.clickWithTrigger {
            if(checkedStyle != 2){
                checkedStyle = 2
                updateCheckedStyle()
            }
        }
        binding.layoutLocation3.clickWithTrigger {
            if(checkedStyle != 3){
                checkedStyle = 3
                updateCheckedStyle()
            }
        }
        binding.layoutLocation4.clickWithTrigger {
            if(checkedStyle != 4){
                checkedStyle = 4
                updateCheckedStyle()
            }
        }
        binding.layoutLocation5.clickWithTrigger {
            if(checkedStyle != 5){
                checkedStyle = 5
                updateCheckedStyle()
            }
        }
        binding.layoutLocation6.clickWithTrigger {
            if(checkedStyle != 6){
                checkedStyle = 6
                updateCheckedStyle()
            }
        }
        binding.layoutLocation7.clickWithTrigger {
            if(checkedStyle != 7){
                checkedStyle = 7
                updateCheckedStyle()
            }
        }
        binding.layoutLocation8.clickWithTrigger {
            if(checkedStyle != 8){
                checkedStyle = 8
                updateCheckedStyle()
            }
        }
    }

    private fun updateCheckedStyle(){
        if (checkedStyle == 1){
            binding.ivChecked1.visibility = View.VISIBLE
        }else{
            binding.ivChecked1.visibility = View.GONE
        }
        if (checkedStyle == 2){
            binding.ivChecked2.visibility = View.VISIBLE
        }else{
            binding.ivChecked2.visibility = View.GONE
        }
        if (checkedStyle == 3){
            binding.ivChecked3.visibility = View.VISIBLE
        }else{
            binding.ivChecked3.visibility = View.GONE
        }
        if (checkedStyle == 4){
            binding.ivChecked4.visibility = View.VISIBLE
        }else{
            binding.ivChecked4.visibility = View.GONE
        }
        if (checkedStyle == 5){
            binding.ivChecked5.visibility = View.VISIBLE
        }else{
            binding.ivChecked5.visibility = View.GONE
        }
        if (checkedStyle == 6){
            binding.ivChecked6.visibility = View.VISIBLE
        }else{
            binding.ivChecked6.visibility = View.GONE
        }
        if (checkedStyle == 7){
            binding.ivChecked7.visibility = View.VISIBLE
        }else{
            binding.ivChecked7.visibility = View.GONE
        }
        if (checkedStyle == 8){
            binding.ivChecked8.visibility = View.VISIBLE
        }else{
            binding.ivChecked8.visibility = View.GONE
        }

    }
    //统一双插屏无需加位置
    fun showDoubleChaPin(){
        AdManager.loadChapin_nocache(
            this,
            666,
            this,
            object : AdLoadListener {
                override fun onAdLoadFailed(ad: IAdView?) {

                }
            })
    }

    override fun onTrigger(adView: IAdView) {

    }

    override fun onAdShow(adView: IAdView) {

    }

    override fun onAdClick(adView: IAdView) {

    }

    override fun onAdClose(adView: IAdView) {

    }

    override fun onError(msg: String) {

    }

    override fun onLoadError(adView: IAdView) {

    }

    override fun onSuccess(adView: IAdView) {

    }

    override fun onLoad(adView: IAdView) {

    }

    override fun onSkipped() {

    }

    override fun onNoAd() {

    }

    override fun onTimeOut() {

    }
}